<?php
class CategoryController extends Controller{
	private $db;
	private $category;
	//构造函数
	public function __init(){
		$this->db=K('Category');  //K 实例化对象(new)
		// 获得所以栏目
		 $this->category=$this->db->getList();
	}
	//显示栏目列表
	public function index(){
		$this->assign('data',$this->category);
		$this->display();
	}
	//添加栏目
	public function add(){
		if(IS_POST){
			if($this->db->addCategory()){
				$this->success('操作成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$this->assign('category',$this->category);
			$this->display();
		}
	}
	// 编辑栏目
	public function edit(){
		if(IS_POST){
			if($this->db->editCategory()){
				$this->success('操作成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			// 获得get传来的cid
			$cid=Q('get.cid',0,'intval');
			//获取当前编辑cid的栏目数据
			$field= $this->db->find($cid); 
			//分配
			$this->assign('field',$field);
			$category=$this->category;
			foreach ($category as $id =>$cat) {
				//当前父栏目设置selected属性
				$category[$id]['selected']=$cat['cid']==$field['pid']?" selected='' ":'';
				//子栏目设置disabled 自身也不能选择
				$category[$id]['disabled']=Data::isChild($category,$cat['cid'],$field['cid']) ||$cat['cid']==$field['cid']?
					" disabled='' ":'';
			}
			$this->assign('category',$category);
			$this->display();
		}
	}

	//删除
	public function del(){
		//Q() 超全局数组 默认($_REQUEST)值，默认值，函数(整形)
		$cid = Q('cid',0,'intval');
		if($this->db->delCategory($cid)){
			$this->success('删除成功','index');
		}else{
			$this->error('删除失败','index');
		}
	}
}


?>